<template>
  <view class="visit">
    <globalNavBar title="预约人管理"/>
    <view class="container  ">
			<uv-form labelPosition="left" :model="postData"  ref="form">
				<uv-form-item label="姓名" labelWidth="140rpx" prop="postData.username" borderBottom required>
				  <uv-input v-model="postData.username" border="none" placeholder="请输入姓名"/>
				</uv-form-item>
				<uv-form-item label="手机号" labelWidth="140rpx" prop="postData.username" borderBottom required>
				  <uv-input v-model="postData.mobile" border="mobile" placeholder="请输入手机号"/>
				</uv-form-item>
				<uv-form-item v-if="idnum_show != 1" label="身份证" labelWidth="140rpx" prop="postData.idnum" borderBottom required>
				  <uv-input v-model="postData.idnum" border="idnum" placeholder="请输入身份证"/>
				</uv-form-item>
				<uv-form-item label="脸部照片" prop="postData.face_image" labelWidth="200rpx" borderBottom
							  @click="uploadFaceImage" >
				  <uv-upload
					:fileList="uploadFileList"
					name="1"
					:maxCount="1"
					:previewFullImage="true"
							@afterRead="afterRead" 
							@delete="deletePic" 
				  />
				</uv-form-item>
			</uv-form>
			<button @click="saveInfo">保存</button>
	</view>
  </view>
</template>

<script>
	import http from '@/utils/request.js'
export default {
  data() {
    return {
		uploadFileList:[],
		postData :{
			username:'',
			mobile:'',
			face_image :'',
			idnum:'',
			id:1
		},
		id : 0,
		idnum_show:1,
    };
  },
  onLoad(options) {
	this.id = options.id
	this.getInfo();
  },
  methods: {
	getInfo(){
		http.post('api/user/appointment_info',{id:this.id}).then((res)=>{
			if(res.data.face_image){
				if(this.uploadFileList[0]){
					this.uploadFileList[0].url = http.common.baseUri+res.data.face_image
				}else{
					this.uploadFileList.push({url:http.common.baseUri+res.data.face_image})
				}
			}else{
				this.uploadFileList.shift()
			}
			this.postData.idnum = res.data.idnum;
			this.postData.username = res.data.username;
			this.postData.mobile = res.data.mobile;
			this.postData.id = res.data.id;
		})
	},
	getConfig(){
		http.get('api/user/max_appointment_count',{}).then((data)=>{
			this.idnum_show = data.data.idnum_show;
		})
	},
	isJson(str){
		if (typeof str === "string") {
		        try {
		          var obj = JSON.parse(str);
		          if (obj && typeof obj === "object") {
		            return true;
		          } else {
		            return false;
		          }
		        } catch (e) {
		          console.log("$isJSON error：", e);
		          return false;
		        }
		      } else {
		        return false;
		      }
	},
	saveInfo(){
		
		if(this.uploadFileList[0]){
			let imageJson = this.uploadFileList[0].url
			if(this.isJson(imageJson)){
				this.postData['face_image'] = JSON.parse(imageJson).data.url;	  
			}else{
				this.postData['face_image'] = this.uploadFileList[0].url.replace(http.common.baseUri,'');
			}  
		} 
		

		if(!this.postData['mobile']){
			uni.showToast({
				title: '请填写手机号'
			})
			return false;
		}
		if(!this.postData['username']){
			uni.showToast({
				title: '请填写姓名'
			})
			return false;
		}
		http.post('api/user/update_appointment_info',this.postData).then((res)=>{
			if(res.code == 1){
				uni.showToast({
					'title':'操作成功',
				});
				setTimeout(()=>{
					uni.navigateTo({
						url:'/pages/booking/appointment_manager'
					})
				},2000)
			}else{
				uni.showToast({
					icon:'error',
					'title':'操作失败',
				})
			}
		})
	},
	deletePic(event) {
		this[`uploadFileList`].splice(event.index, 1)
	},
	// 新增图片
	async afterRead(event) {
		// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
		let lists = [].concat(event.file)
		let fileListLen = this[`uploadFileList`].length
		lists.map((item) => {
			this[`uploadFileList`].push({
				...item,
				status: 'uploading',
				message: '上传中'
			})
		})
		for (let i = 0; i < lists.length; i++) {
			const result = await this.uploadFilePromise(lists[i].url)
			let item = this[`uploadFileList`][fileListLen]
			this[`uploadFileList`].splice(fileListLen, 1, Object.assign(item, {
				status: 'success',
				message: '',
				url: result
			}))
			fileListLen++
		}
	},
	uploadFilePromise(url) {
		return new Promise((resolve, reject) => {
			let a = uni.uploadFile({
				url: http.common.baseUrl+'api/common/upload', // 仅为示例，非真实的接口地址
				filePath: url,
				name: 'file',
				 header: {
					'token': uni.getStorageSync('token') // 设置认证token
				  },
				success: (res) => {
					setTimeout(() => {
						resolve(res.data)
					}, 1000)
				}
			});
		})
	},
	
  }
}
</script>

<style lang="scss" scoped>
	
	.flex {
		display: flex;
	}
	.flex_center {
		display: flex;
		justify-content: center;
	}
	.flex_wrap {
		flex-wrap: wrap;
	}
	.flex_column {
		flex-direction: column;
	}
	.mt-10 {
		margin-top: 10rpx;
	}
	.container {
		padding: 20rpx;

	}
	
	
	
	
</style>
